import {dicAll} from "@/axios/apis.ts"
import store from '@/store/index.tsx'
import {changeDics} from '@/store/DIC.tsx';

export const getDicAll = async () => {
    const res = await dicAll()
    if (+res.code === 200) {
        store.dispatch(changeDics(res.data))
    }
}

// 根据dicCode和dicItemCode获取dicItemName
export const findDicNameForDicCode = (dicCode, dicItemCode) => {
    if (!dicCode || !dicItemCode) return dicItemCode
    const list = store.getState().DICSlice?.dics
    if (!list || !list.length) return dicItemCode
    const item = list.find(item => item.dicCode === dicCode)
    if (!item || !item.itemList) return dicItemCode
    const childItem = item.itemList.find(item => item.dicItemCode === dicItemCode)
    return childItem?.dicItemName || dicItemCode
}

// 根据dicCode获取字典
export const getDicOnCode = (CODE: string) => {
    const list = store.getState().DICSlice?.dics
    if (!list || !Array.isArray(list)) return []
    return list.find(item => item.dicCode === CODE)?.itemList || []
}